package cn.yangke.basic.controller;

import cn.yangke.basic.dto.LoginDto;
import cn.yangke.basic.dto.WechatDto;
import cn.yangke.basic.exception.BusinessException;
import cn.yangke.basic.service.ILoginService;
import cn.yangke.basic.util.AjaxResult;
import cn.yangke.user.mapper.WxuserMapper;
import cn.yangke.user.service.IWxuserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

/**
 * @Author YangKe
 * @Date 2022/6/18 17:05
 */
@RestController
@RequestMapping("/login")
public class LoginController {
    @Autowired
    private ILoginService loginService;


    @PostMapping("/wechat")
    private AjaxResult wechat(@RequestBody WechatDto dto){
        try {
            return loginService.wechat(dto);
        } catch (BusinessException e) {
            e.printStackTrace();
            return AjaxResult.getAjax().setSuccess(false).setMessage(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.getAjax().setSuccess(false).setMessage("保存对象失败！"+e.getMessage());
        }
    }

    /**
     * 后台登录
     * @param dto
     * @return
     */
    @PostMapping("/account")
    private AjaxResult account(@RequestBody LoginDto dto){
        try {
            Map<String, Object> map = loginService.account(dto);
            return AjaxResult.getAjax().setResultObj(map);
        } catch (BusinessException e) {
            e.printStackTrace();
            return AjaxResult.getAjax().setSuccess(false).setMessage(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.getAjax().setSuccess(false).setMessage("保存对象失败！"+e.getMessage());
        }
    }

    /**
     * 微信绑定
     * @param dto
     * @return
     */
    @PostMapping("/wechat/binder")
    public AjaxResult binder(@RequestBody WechatDto dto){
        try {
            Map<String, Object> map = loginService.binder(dto);
            return AjaxResult.getAjax().setResultObj(map);
        } catch (BusinessException e) {
            e.printStackTrace();
            return AjaxResult.getAjax().setSuccess(false).setMessage(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.getAjax().setSuccess(false).setMessage("保存对象失败！"+e.getMessage());
        }
    }


}
